import csv
from datetime import datetime

from matplotlib import pyplot as plt

filename = 'D:\\Python\\projects\\python_01\\files\\sitka_weather_2014.csv'
with open(filename) as f:
    reader = csv.reader(f)
    header_row = next(reader)

    # 定义存放数据的空数组
    dates, precipitationIns = [], []
    for row in reader:
        try:
            # 获取日期
            current_date = datetime.strptime(row[0], "%Y-%m-%d")
            # 转换为整数类型
            precipitationIn = int(row[18])

        except ValueError:
            print(current_date,precipitationIn,'missing data')
        else:
            dates.append(current_date)
            precipitationIns.append(precipitationIn)

# 绘制图表
fig = plt.figure(dpi=128, figsize=(10, 6))
plt.plot(dates, precipitationIns, c='red', alpha=0.5)
# fill_between 用蓝色填充函数之间的区域
plt.fill_between(dates, precipitationIns, facecolor='blue', alpha=0.1)

# 设置图形的格式
title = "PrecipitationIn - 2014"
plt.title(title, fontsize=20)
plt.xlabel('Data', fontsize=16)
# 改变x轴坐标的显示方法可以斜着表示,不用平着挤一堆
fig.autofmt_xdate()
plt.ylabel("PrecipitationIn", fontsize=16)
plt.tick_params(axis='both', which='major', labelsize=16)

plt.show()
